package com.android.execucaocampo;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;

import com.android.beans.Conta;
import com.android.beans.OrdemServico;
import com.android.data.RepositorioConta;
import com.android.data.RepositorioOrdemServico;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.ListView;

public class DebitosActivity extends Activity {

	public ListView lista;
	ArrayList<Conta> arrayConta;
	
	DecimalFormat df = new DecimalFormat("#,###.00");
    SimpleDateFormat out = new SimpleDateFormat("dd/MM/yyyy");
    
    /** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.debitos);
		
		RepositorioConta repConta = new RepositorioConta(getApplicationContext());
		RepositorioOrdemServico repOS = new RepositorioOrdemServico(getApplicationContext());
		
		//RECUPERAR VALORES DA TELA ANTERIOR
        Bundle b = this.getIntent().getExtras();
        
        OrdemServico os = repOS.procurar(Integer.parseInt(AbaOrdemServicoActivity.numOS));

		arrayConta = repConta.listar(" WHERE codigoordemservico = '" + os.getCodigo() + "'");
		 
        ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;
		
		for (Conta conta : arrayConta) {
			map = new HashMap<String, String>();
			
			map.put("codigo", String.valueOf(conta.getCodigo()));
			map.put("flagp", String.valueOf(conta.getFlagPago()));
			map.put("dtVencimento", out.format(conta.getDataVencimento()));
			map.put("valor", "R$ " + df.format(conta.getValor()));
			map.put("mesAno", conta.getMesAno());
			map.put("tipoConta", conta.getDescricaoTipoConta());
		
			mylist.add(map);
		}
		
		lista = (ListView) findViewById(R.id.listacontas);
		
		lista.setAdapter(new SimpleAdapterWithCheck(this, mylist, R.layout.rowconta,
            new String[] {"codigo", "flagp","dtVencimento", "mesAno", "valor", "Tipo"}, 
            new int[] {R.id.codigoconta, R.id.flag, R.id.dtvencimento, R.id.mesanovenc, R.id.valor, R.id.tipoConta}) {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				// TODO Auto-generated method stub		
				final View view = super.getView(position, convertView, parent);
						
				HashMap<String, String> map = (HashMap<String, String>) super.getItem(position);
				
				final CheckBox checkBox = (CheckBox) view.findViewById(R.id.flagpago);
				checkBox.setTag(map.get("codigo"));
				
				if (map.get("flagp").equals("0")) {
					checkBox.setChecked(false);
				} else if (map.get("flagp").equals("1")) {
					checkBox.setChecked(true);
				}
				
				checkBox.setOnClickListener(new OnClickListener() {					
					public void onClick(View v) {				
						if (checkBox.isChecked()) {
							//checkBox.setChecked(false);
							//CheckBox cb = (CheckBox) v ;    
							
							RepositorioConta repConta = new RepositorioConta(getApplicationContext());
							Conta contaAux = repConta.procurar(checkBox.getTag().toString());
							
							contaAux.setFlagPago(1);
							repConta.atualizar(contaAux);							
						} else {
							//checkBox.setChecked(true);
//							CheckBox cb = (CheckBox) v ;  
//							HashMap<String, String> map = (HashMap<String, String>) cb.getTag();
							
							RepositorioConta repConta = new RepositorioConta(getApplicationContext());
							Conta contaAux = repConta.procurar(checkBox.getTag().toString());
							
							contaAux.setFlagPago(0);
							repConta.atualizar(contaAux);							
						}
					}
				});
				
				return view;
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}
}